package org.liufeng.weixin.main;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.liufeng.weixin.pojo.AccessToken;  
import org.liufeng.weixin.pojo.Button;  
import org.liufeng.weixin.pojo.CommonButton;  
import org.liufeng.weixin.pojo.ComplexButton;  
import org.liufeng.weixin.pojo.Menu;  
import org.liufeng.weixin.util.WeixinUtil;  
import org.slf4j.Logger;  
import org.slf4j.LoggerFactory;  


import com.thoughtworks.xstream.XStream;


  
/** 
 * 菜单管理器类 
 *  
 * @author liufeng 
 * @date 2013-08-08 
 */  
public class MenuManager {  
    private static Logger log = LoggerFactory.getLogger(MenuManager.class);  
	private XStream xstream = null;
    private ObjectOutputStream  out = null;
    private ObjectInputStream in = null;
    public static void main(String[] args) {  

    }
    

    @Before    
    public void init() {        
        try {            
        		xstream = new XStream();
        		//xstream = new XStream(new DomDriver());
        		// 需要xpp3 jar        
        	} catch (Exception e) {            
        		e.printStackTrace();
        }       

 
    }        
    /**     
     * * <b>function:</b>释放对象资源     * 
     * @author hoojo     * 
     * @createDate Nov 27, 2010 12:16:38 PM     */    
    
    @After    
    public void destory() {        
        xstream = null;

        try {            
        	if (out != null) {                
        		out.flush();
                out.close();
            }            
        	if (in != null) {                
        		in.close();
            }       
        } catch (IOException e) {           
        	e.printStackTrace();
        }        
        System.gc();
    }
        

    public void createMenu (){
    	System.out.println("-------------createMenu------------");
        // 第三方用户唯一凭证   
        String appId = "wxb646688880f54ca8";  
        // 第三方用户唯一凭证密钥   
        String appSecret = "f83ee621b022433097ace94a19543661";  
  
        // 调用接口获取access_token   
        AccessToken at = WeixinUtil.getAccessToken(appId, appSecret);  
  
        if (null != at) {  
            // 调用接口创建菜单   
            int result = WeixinUtil.createMenu(getMenuFromXml(), at.getToken());  
        	
            // 判断菜单创建结果   
            if (0 == result)  
                log.info("菜单创建成功！");  
            else  
                log.info("菜单创建失败，错误码：" + result);  
        }  
        
    }
    
    
    @Test
    public void listMenu (){
    	System.out.println("-------------listMenu------------");
        // 第三方用户唯一凭证   
        // 第三方用户唯一凭证   
        String appId = "wxb646688880f54ca8";  
        // 第三方用户唯一凭证密钥   
        String appSecret = "f83ee621b022433097ace94a19543661";  
  
        // 调用接口获取access_token   
        AccessToken at = WeixinUtil.getAccessToken(appId, appSecret);  
  
        if (null != at) {  
            // 调用接口创建菜单   
            int result = WeixinUtil.listMenu(  at.getToken() );  
  
            // 判断菜单创建结果   
            if (0 == result)  
                log.info("查询创建成功！");  
            else  
                log.info("查询菜单失败，错误码：" + result);
        }     
    }
    
    


    public void deleteMenu (){
    	System.out.println("-------------deleteMenu------------");
        // 第三方用户唯一凭证   
        // 第三方用户唯一凭证   
        String appId = "wxb646688880f54ca8";  
        // 第三方用户唯一凭证密钥   
        String appSecret = "f83ee621b022433097ace94a19543661";  
        // 调用接口获取access_token   
        AccessToken at = WeixinUtil.getAccessToken(appId, appSecret);  
  
        if (null != at) {  
            // 调用接口创建菜单   
            int result = WeixinUtil.deleteMenu(  at.getToken() );  
  
            // 判断菜单创建结果   
            if (0 == result)  
                log.info("删除成功！");  
            else  
                log.info("删除菜单失败，错误码：" + result);
        }     
    }    
    
    /** 
     * 组装菜单数据 
     *  
     * @return 
     */


    public void showMenu() {  
    	System.out.println("123");
        CommonButton btn11 = new CommonButton();  
        btn11.setName("天气预报");  
        btn11.setType("click");  
        btn11.setKey("11");  
  
        CommonButton btn12 = new CommonButton();  
        btn12.setName("公交查询");  
        btn12.setType("click");  
        btn12.setKey("12");  
  
        CommonButton btn13 = new CommonButton();  
        btn13.setName("周边搜索");  
        btn13.setType("click");  
        btn13.setKey("13");  
  
        CommonButton btn14 = new CommonButton();  
        btn14.setName("历史上的今天");  
        btn14.setType("click");  
        btn14.setKey("14");  
  
        CommonButton btn21 = new CommonButton();  
        btn21.setName("歌曲点播");  
        btn21.setType("click");  
        btn21.setKey("21");  
  
        CommonButton btn22 = new CommonButton();  
        btn22.setName("经典游戏");  
        btn22.setType("click");  
        btn22.setKey("22");  
  
        CommonButton btn23 = new CommonButton();  
        btn23.setName("美女电台");  
        btn23.setType("click");  
        btn23.setKey("23");  
  
        CommonButton btn24 = new CommonButton();  
        btn24.setName("人脸识别");  
        btn24.setType("click");  
        btn24.setKey("24");  
  
        CommonButton btn25 = new CommonButton();  
        btn25.setName("聊天唠嗑");  
        btn25.setType("click");  
        btn25.setKey("25");  
  
        CommonButton btn31 = new CommonButton();  
        btn31.setName("Q友圈");  
        btn31.setType("click");  
        btn31.setKey("31");  
  
        CommonButton btn32 = new CommonButton();  
        btn32.setName("电影排行榜");  
        btn32.setType("click");  
        btn32.setKey("32");  
  
        CommonButton btn33 = new CommonButton();  
        btn33.setName("幽默笑话");  
        btn33.setType("click");  
        btn33.setKey("33"); 
  
        ComplexButton mainBtn1 = new ComplexButton();  
        mainBtn1.setName("生活助手");  
        mainBtn1.setSub_button(new CommonButton[] { btn11, btn12, btn13, btn14 });  
  
        ComplexButton mainBtn2 = new ComplexButton();  
        mainBtn2.setName("休闲驿站");  
        mainBtn2.setSub_button(new CommonButton[] { btn21, btn22, btn23, btn24, btn25 });  
  
        ComplexButton mainBtn3 = new ComplexButton();  
        mainBtn3.setName("更多体验");
        
        mainBtn3.setSub_button(new CommonButton[] { btn31, btn32, btn33 });  
  
        /** 
         * 这是公众号xiaoqrobot目前的菜单结构，每个一级菜单都有二级菜单项<br> 
         *  
         * 在某个一级菜单下没有二级菜单的情况，menu该如何定义呢？<br> 
         * 比如，第三个一级菜单项不是“更多体验”，而直接是“幽默笑话”，那么menu应该这样定义：<br> 
         * menu.setButton(new Button[] { mainBtn1, mainBtn2, btn33 }); 
         */  
        Menu menu = new Menu();  
        menu.setButton(new Button[] { mainBtn1, mainBtn2, btn33 });  
        
        System.out.println("xstream.toXML(menu); = " + xstream.toXML(menu) );
       
    }      
    
    /** 
     * 组装菜单数据 
     *  
     * @return 
     */

    
    public  Menu getMenu() {  
    	System.out.println("123");
        CommonButton btn11 = new CommonButton();  
        btn11.setName("天气预报");  
        btn11.setType("click");  
        btn11.setKey("11");  
  
        CommonButton btn12 = new CommonButton();  
        btn12.setName("公交查询");  
        btn12.setType("click");  
        btn12.setKey("12");  
  
        CommonButton btn13 = new CommonButton();  
        btn13.setName("周边搜索");  
        btn13.setType("click");  
        btn13.setKey("13");  
  
        CommonButton btn14 = new CommonButton();  
        btn14.setName("历史上的今天");  
        btn14.setType("click");  
        btn14.setKey("14");  
  
        CommonButton btn21 = new CommonButton();  
        btn21.setName("歌曲点播");  
        btn21.setType("click");  
        btn21.setKey("21");  
  
        CommonButton btn22 = new CommonButton();  
        btn22.setName("经典游戏");  
        btn22.setType("click");  
        btn22.setKey("22");  
  
        CommonButton btn23 = new CommonButton();  
        btn23.setName("美女电台");  
        btn23.setType("click");  
        btn23.setKey("23");  
  
        CommonButton btn24 = new CommonButton();  
        btn24.setName("人脸识别");  
        btn24.setType("click");  
        btn24.setKey("24");  
  
        CommonButton btn25 = new CommonButton();  
        btn25.setName("聊天唠嗑");  
        btn25.setType("click");  
        btn25.setKey("25");  
  
        CommonButton btn31 = new CommonButton();  
        btn31.setName("Q友圈");  
        btn31.setType("click");  
        btn31.setKey("31");  
  
        CommonButton btn32 = new CommonButton();  
        btn32.setName("电影排行榜");  
        btn32.setType("click");  
        btn32.setKey("32");  
  
        CommonButton btn33 = new CommonButton();  
        btn33.setName("幽默笑话");  
        btn33.setType("click");  
        btn33.setKey("33"); 
  
        ComplexButton mainBtn1 = new ComplexButton();  
        mainBtn1.setName("生活助手");  
        mainBtn1.setSub_button(new CommonButton[] { btn11, btn12, btn13, btn14 });  
  
        ComplexButton mainBtn2 = new ComplexButton();  
        mainBtn2.setName("休闲驿站");  
        mainBtn2.setSub_button(new CommonButton[] { btn21, btn22, btn23, btn24, btn25 });  
  
        ComplexButton mainBtn3 = new ComplexButton();  
        mainBtn3.setName("更多体验");
        
        mainBtn3.setSub_button(new CommonButton[] { btn31, btn32, btn33 });  
  
        /** 
         * 这是公众号xiaoqrobot目前的菜单结构，每个一级菜单都有二级菜单项<br> 
         *  
         * 在某个一级菜单下没有二级菜单的情况，menu该如何定义呢？<br> 
         * 比如，第三个一级菜单项不是“更多体验”，而直接是“幽默笑话”，那么menu应该这样定义：<br> 
         * menu.setButton(new Button[] { mainBtn1, mainBtn2, btn33 }); 
         */  
        Menu menu = new Menu();  
        menu.setButton(new Button[] { mainBtn1, mainBtn2, mainBtn3 });  
        xstream.toXML(menu);
        System.out.println("menu = " +menu.toString());
        return menu;  
    }  
    
    public static void failRed(String string)  {        
    	System.err.println(string);
    }
    public static void fail(String string) {        
    	System.out.println(string);
    }        
        

    public  Menu getMenuFromXml() {  
    	Menu menu =null;
    	try {        

	        failRed("---------ObjectInputStream## XML --> javaObject---------");
	        File input = new File( (MenuManager.class.getResource("")+"menu.xml").replaceFirst("file:/", "")  );
	        InputStream is = null;
			try {
				is = new FileInputStream(input);
		        
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			menu = (Menu) xstream.fromXML(is); 
	        System.out.println(menu);

	    } 
	        catch (Exception e) {        
	        e.printStackTrace();
	    }
       // Menu menu = new Menu();  
        //menu.setButton(new Button[] { mainBtn1, mainBtn2, mainBtn3 });  
  
        return menu;  
    }      
}